/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/
#include "safemap.h"
#include <cassert>
#include <iostream>
SafeMap::SafeMap(): logger_(log4cxx::Logger::getLogger("server.safemap"))
{


}

SafeMap::~SafeMap()
{

}

void SafeMap::registerLoggedUser(User* user,BlockingQueue *socket){
  Lock l(mutex);
  assert(user != NULL && "user pointer is null!");
  LOG4CXX_TRACE(logger_, "registering user socket for "<<user->getId());
  usersSockets[user->getId()]=socket;
}

void SafeMap::logoutUser(User* user){
  Lock l(mutex);

  usersSockets.erase(user->getId());
}

std::list<BlockingQueue*> SafeMap::findQueues(Lesson& lesson){
  Lock l(mutex);
  std::list<BlockingQueue*> sockets;

  for(std::list<User*>::iterator it=lesson.getStudents().begin(); it!=lesson.getStudents().end();it++){
      User* user=(User*)*it;
      assert(user!= NULL && "user pointer is null!");
      if(user->isListeningLesson()){
      LOG4CXX_TRACE(logger_, "finding socket for "<<user->getId());
      BlockingQueue* socket=usersSockets[user->getId()];
          if(socket!=NULL){
          sockets.push_back(socket);
          LOG4CXX_TRACE(logger_, "socket founded");
          }
      }
  }
  for(std::list<User*>::iterator it=lesson.getInstructors().begin(); it!=lesson.getInstructors().end();it++){
        User* user=(User*)*it;
        assert(user!= NULL && "user pointer is null!");
        LOG4CXX_TRACE(logger_, "finding socket for "<<user->getId());
        BlockingQueue* socket=usersSockets[user->getId()];
            if(socket!=NULL){
            sockets.push_back(socket);
            LOG4CXX_TRACE(logger_, "socket founded");
            }

    }
  return sockets;
}

std::list<BlockingQueue*> SafeMap::findStudentsQueues(Lesson& lesson){
  Lock l(mutex);
  std::list<BlockingQueue*> sockets;

  for(std::list<User*>::iterator it=lesson.getStudents().begin(); it!=lesson.getStudents().end();it++){
      User* user=(User*)*it;
      assert(user!= NULL && "user pointer is null!");
      if(user->isListeningLesson()){
      LOG4CXX_TRACE(logger_, "finding socket for "<<user->getId());
      BlockingQueue* socket=usersSockets[user->getId()];
          if(socket!=NULL){
          sockets.push_back(socket);
          LOG4CXX_TRACE(logger_, "socket founded");
          }
      }
  }
  return sockets;
}
